package gsw.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import gsw.demo.bean.JsonRoot;
import gsw.demo.bean.Parent;
import gsw.tool.json.JSON;
import gsw.tool.json.TypeReference;

public class MainActivity extends AppCompatActivity {

    TextView showResult;
    String bigJson;//json格式字符串
    long start;//开始测试时间

    Parent.Child child1;
    Parent.Child child2;
    Parent.Child child3;
    Parent.Child child4;
    List<Parent.Child> list1;
    List<Parent.Child> list2;
    List<List<Parent.Child>> list3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showResult = findViewById(R.id.show_result);
        init();
        getFile();
    }

    /**
     * 读取json文件
     */
    private void getFile() {
        try {
            InputStream is = getAssets().open("test.json");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[2048];
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            is.close();
            bigJson = new String(bos.toByteArray());
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化数据
     */
    private void init() {
        child1 = new Parent.Child("王1", 101);
        child2 = new Parent.Child("王2", 102);
        child3 = new Parent.Child("王3", 103);
        child4 = new Parent.Child("王4", 104);

        list1 = new ArrayList<>();
        list2 = new ArrayList<>();
        list3 = new ArrayList<>();
        list1.add(child1);
        list1.add(child2);
        list2.add(child3);
        list2.add(child4);
        list3.add(list1);
        list3.add(list2);
    }

    /**
     * 测试Json框架的功能
     */
    public void testJson(View view) {
        StringBuilder result = new StringBuilder();//结果

        String json1 = JSON.toJSONString(child1);
        child1 = JSON.parse(json1, Parent.Child.class);
        result.append("序列化对象:" + json1);
        result.append("\n反序列化对象:" + child1);

        String json2 = JSON.toJSONString(list1);
        list1 = JSON.parse(json2, new TypeReference<List<Parent.Child>>() {
        }.getType());
        result.append("\n\n序列化集合:" + json2);
        result.append("\n反序列化集合：" + list1);

        String json3 = JSON.toJSONString(list3);
        list3 = JSON.parse(json3, new TypeReference<List<List<Parent.Child>>>() {
        }.getType());
        result.append("\n\n序列化复杂集合:" + json3);
        result.append("\n反序列化复杂集合：" + list3);

        showResult.setText(result.toString());
        Log.v("test2008", result.toString());
    }

    /**
     * 测试Json框架的速度
     */
    public void testFast(View view) {
        StringBuilder result = new StringBuilder();//结果

        start = System.currentTimeMillis();
        String json1 = JSON.toJSONString(child1);
        result.append("ToolJson序列化对象耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        String json2 = com.alibaba.fastjson.JSON.toJSONString(child1);
        result.append("\nFastJson反序列化对象耗时:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        child1 = JSON.parse(json1, Parent.Child.class);
        result.append("\n\nToolJson反序列化对象耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        child1 = com.alibaba.fastjson.JSON.parseObject(json2, Parent.Child.class);
        result.append("\nFastJson反序列化对象耗时:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        String json3 = JSON.toJSONString(list1);
        result.append("\n\nToolJson序列化集合耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        String json4 = com.alibaba.fastjson.JSON.toJSONString(list1);
        result.append("\nFastJson序列化集合耗时:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        list1 = JSON.parse(json3, new TypeReference<List<Parent.Child>>() {
        }.getType());
        result.append("\n\nToolJson反序列化集合耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        list1 = com.alibaba.fastjson.JSON.parseObject(json4, new com.alibaba.fastjson
                .TypeReference<List<Parent.Child>>() {
        }.getType());
        result.append("\nFastJson反序列化集合耗时:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        JsonRoot parse1 = JSON.parse(bigJson, JsonRoot.class);
        result.append("\n\nToolJson反序列化大数据耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        JsonRoot parse2 = com.alibaba.fastjson.JSON.parseObject(bigJson, JsonRoot.class);
        result.append("\nFastJson反序列化大数据耗时:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        String json5 = JSON.toJSONString(parse1);
        result.append("\n\nToolJson序列化大数据耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        String json6 = com.alibaba.fastjson.JSON.toJSONString(parse2);
        result.append("\nFastJson序列化大数据耗时:" + (System.currentTimeMillis() - start));

        showResult.setText(result.toString());
        Log.v("test2008", result.toString());
    }

    class Test {
        private String name;
        private int age;
        private Test test;

        public Test() {
        }

        public Test(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public Test(String name, int age, Test test) {
            this.name = name;
            this.age = age;
            this.test = test;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Test getTest() {
            return test;
        }

        public void setTest(Test test) {
            this.test = test;
        }
    }
}
